package com.beidasoft.web.util;

import java.util.Collection;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtility {
    public StringUtility() {
    }

    public static UUID emptyUUID() {
        return UUID.fromString("00000000-0000-0000-0000-000000000000");
    }

    public static String arrayToString(Object[] arr) {
        return arrayToString(arr, "", ",", "");
    }

    public static boolean isNullOrEmpty(String s) {
        return s == null || s.equals("");
    }

    public static boolean isInteger(Object o) {
        try {
            Integer.parseInt(o.toString());
            return true;
        } catch (Exception var2) {
            return false;
        }
    }

    public static boolean isDouble(Object o) {
        try {
            Double.parseDouble(o.toString());
            return true;
        } catch (Exception var2) {
            return false;
        }
    }

    public static boolean isUUID(Object o) {
        try {
            UUID.fromString(o.toString());
            return true;
        } catch (Exception var2) {
            return false;
        }
    }

    public static String arrayToString(Object[] arr, String prefix, String midfix, String postfix) {
        StringBuffer sb = new StringBuffer();
        sb.append(prefix);
        int i = 1;
        Object[] var6 = arr;
        int var7 = arr.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            Object o = var6[var8];
            sb.append(o == null ? "" : o.toString());
            if (i < arr.length) {
                sb.append(midfix);
            }

            ++i;
        }

        sb.append(postfix);
        return sb.toString();
    }

    public static String arrayToString(Collection arr) {
        return arrayToString(arr, "", ",", "");
    }

    public static String arrayToString(Collection arr, String prefix, String midfix, String postfix) {
        return arrayToString(arr.toArray(), prefix, midfix, postfix);
    }

    public static boolean isLong(Object o) {
        try {
            Long.parseLong(o.toString());
            return true;
        } catch (Exception var2) {
            return false;
        }
    }

    public static <T> int findValueInCollction(int startPosition, T[] arr, T value) {
        for(int i = startPosition; i < arr.length; ++i) {
            if (arr[i].equals(value)) {
                return i;
            }
        }

        return -1;
    }

    public static boolean equalsIgnoreNull(String s1, String s2) {
        if (isNullOrEmpty(s1)) {
            if (isNullOrEmpty(s2)) {
                return true;
            }
        } else if (s1.equals(s2)) {
            return true;
        }

        return false;
    }

    public static String convertNulltoString(Object obj) {
        return obj != null && !obj.equals("undefined") && !obj.equals("null") ? obj.toString() : "";
    }

    public static String convertNulltoDefault(Object obj, Object obj2) {
        return obj == null ? (obj2 == null ? "" : obj2.toString()) : obj.toString();
    }

    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();

        for(int i = 0; i < bs.length; ++i) {
            int bit = (bs[i] & 240) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 15;
            sb.append(chars[bit]);
        }

        return sb.toString().trim();
    }

    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];

        for(int i = 0; i < bytes.length; ++i) {
            int n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte)(n & 255);
        }

        return new String(bytes);
    }

    public static String byte2HexStr(byte[] b) {
        String stmp = "";
        StringBuilder sb = new StringBuilder("");

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            sb.append(stmp.length() == 1 ? "0" + stmp : stmp);
        }

        return sb.toString().toUpperCase().trim();
    }

    public static byte[] hexStr2Bytes(String src) {
        int l = src.length() / 2;
        System.out.println(l);
        byte[] ret = new byte[l];

        for(int i = 0; i < l; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            ret[i] = Byte.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
        }

        return ret;
    }

    public static String strToUnicode(String strText) throws Exception {
        StringBuilder str = new StringBuilder();

        for(int i = 0; i < strText.length(); ++i) {
            char c = strText.charAt(i);
            String strHex = Integer.toHexString(c);
            if (c > 128) {
                str.append("\\u" + strHex);
            } else {
                str.append("\\u00" + strHex);
            }
        }

        return str.toString();
    }

    public static String unicodeToString(String hex) {
        int t = hex.length() / 6;
        StringBuilder str = new StringBuilder();

        for(int i = 0; i < t; ++i) {
            String s = hex.substring(i * 6, (i + 1) * 6);
            String s1 = s.substring(2, 4) + "00";
            String s2 = s.substring(4);
            int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
            char[] chars = Character.toChars(n);
            str.append(new String(chars));
        }

        return str.toString();
    }

    public static String toLowerCaseFirstOne(String s) {
        return Character.isLowerCase(s.charAt(0)) ? s : Character.toLowerCase(s.charAt(0)) + s.substring(1);
    }

    public static String toUpperCaseFirstOne(String s) {
        return Character.isUpperCase(s.charAt(0)) ? s : Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }

    public static String nameFormat(String name) {
        if (name.length() == 2) {
            char[] name_Array = name.toCharArray();
            return name_Array[0] + "　" + name_Array[1];
        } else {
            return name;
        }
    }

    public static boolean isNullOrEmpty(Object object) {
        if (object == null) {
            return true;
        } else {
            return object.toString().equals("");
        }
    }

    public static String StringStartTrim(String stream, String trim) {
        if (stream != null && stream.length() != 0 && trim != null && trim.length() != 0) {
            String regPattern = "[" + trim + "]*+";
            Pattern pattern = Pattern.compile(regPattern, 2);
            Matcher matcher = pattern.matcher(stream);
            if (matcher.lookingAt()) {
                int end = matcher.end();
                stream = stream.substring(end);
            }

            return stream;
        } else {
            return stream;
        }
    }

    public static boolean compare(String str1, String str2) {
        return convertNulltoString(str1).equalsIgnoreCase(convertNulltoString(str2));
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().equals("");
    }

    public static boolean isNotNull(String str) {
        return !isEmpty(str);
    }

    public static int strConvertIntOrDefault(String str, int defVal) {
        if (isEmpty(str)) {
            return defVal;
        } else {
            int result;
            try {
                result = Integer.parseInt(str);
            } catch (NumberFormatException var4) {
                result = defVal;
            }

            return result;
        }
    }

    public static int strConvertInt(String str) {
        return strConvertIntOrDefault(str, 0);
    }
}
